# noinspection PyMethodMayBeStatic

from rest_framework.response import Response
from rest_framework import viewsets, status

from djangoProject.base.resp import data_response, error_response
from .models import User, UserProfile
from .serializers import UserSerializer, UserProfileSerializer, RegisterSerializer
from rest_framework.views import APIView
import pdb


class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class UserProfileViewSet(viewsets.ModelViewSet):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer

class LoginView(APIView):
    def post(self, request):

        username = request.data['username']
        password = request.data['password']
        user = User.objects.get(username=username, password=password)
        profile = UserProfile.objects.get(user=user)
        if profile:
            return Response(UserSerializer(profile).data, status=status.HTTP_200_OK)
        else:
            return Response({'error': 'Invalid credentials'}, status=status.HTTP_200_OK)


class RegisterView(APIView):
    def post(self, request):

        serializer = RegisterSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return data_response( message='注册成功', status_code=status.HTTP_200_OK)
        return error_response(status.HTTP_200_OK, serializer.errors)


class SetView(APIView):
    # noinspection PyMethodMayBeStatic
    def get(self, request):

        return Response({'status': 'ok'}, status=status.HTTP_200_OK)
    def post(self, request):
        return Response({'status': 'no', 'message': 'bad request'}, status=status.HTTP_200_OK)